package cn.jaychang.demo.resource.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
import org.springframework.security.oauth2.provider.token.store.JwtTokenStore;

/**
 * 令牌的配置
 *
 * @author jaychang
 * @since 2024/5/20
 **/
@Configuration
public class AccessTokenConfig {

    private static final String SIGN_KEY = "jaychang!@#abc123;'/{}|";

    /**
     * 令牌存储策略
     *
     * @return
     */
    @Bean
    public TokenStore tokenStore() {
        // 暂时使用内存存储，一旦服务重启令牌失效，后续可以改用数据库存储
        return new JwtTokenStore(jwtAccessTokenConverter());
    }

    /**
     * JwtAccessTokenConverter
     * TokenEnhancer的子类，再JWT编码的令牌值和OAuth身份验证信息之间进行转换
     *
     * @return
     */
    @Bean
    public JwtAccessTokenConverter jwtAccessTokenConverter() {
        JwtAccessTokenConverter tokenConverter = new JwtAccessTokenConverter();
        tokenConverter.setSigningKey(SIGN_KEY);
        return tokenConverter;
    }
}
